package com.newworld.dmsapp2.register;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.BaseActivity;
import com.newworld.dmsapp2.LoginActivity;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.HouseManageListActivity;
import com.newworld.dmsapp2.ui.view.choosecity.ChoosecityMainActivity;
import com.newworld.dmsapp2.util.Config;

public class HouseProvingActivity extends BaseActivity implements
		OnClickListener {
	private EditText edit_city, edit_community, edit_housenumber;
	private TextView titlebar_invitecode_center_title;
	private Button  titlebar_both_right_invitecode_relate,
			btn_next;
	private View timeline_view_2;
	private ImageView timeline_3stage_image_3;
	private TextView timeline_text_3;
	
	private Intent intent = new Intent();;
	private Bundle bundle;
	private String cityID = "";
	private String projectID = "";
	private String houseID = "";
	private String projectStr = "";
	private String houseStr = "";
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register_house_proving);
		initViews();
		initEvents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void initViews() {
		
		titlebar_invitecode_center_title = (TextView) findViewById(R.id.titlebar_invitecode_center_title);
		titlebar_invitecode_center_title.setText(Config.TITLE_HOUSE_ATTATCH);
		LinearLayout leftlayout = (LinearLayout) findViewById(R.id.titlebar_invitecode_left_layout);
		leftlayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserMessageActivity.INTENT_FLAG == 0) {
					intentActivity(HouseProvingActivity.this,
							LoginActivity.class);
				} else if (UserMessageActivity.INTENT_FLAG == 1) {
					intentActivity(HouseProvingActivity.this,
							HouseManageListActivity.class);
				}
			}
		});

		titlebar_both_right_invitecode_relate = (Button) findViewById(R.id.titlebar_both_right_invitecode_relate);
		btn_next = (Button) findViewById(R.id.house_btn_next);
		edit_city = (EditText) findViewById(R.id.house_edit_city);
		edit_community = (EditText) findViewById(R.id.house_edit_community);
		edit_housenumber = (EditText) findViewById(R.id.house_edit_housenumber);
		timeline_view_2 = (View) findViewById(R.id.timeline_view_2);
		timeline_3stage_image_3 = (ImageView) findViewById(R.id.timeline_3stage_image_3);
		timeline_text_3 = (TextView) findViewById(R.id.timeline_text_3);
		if (UserMessageActivity.INTENT_FLAG == 1) {
			timeline_view_2.setVisibility(View.GONE);
			timeline_3stage_image_3.setVisibility(View.GONE);
			timeline_text_3.setVisibility(View.GONE);
		}

	}

	@Override
	protected void initEvents() {
		titlebar_both_right_invitecode_relate.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		edit_city.setOnClickListener(this);
		edit_community.setOnClickListener(this);
		edit_housenumber.setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titlebar_both_right_invitecode_relate:

			Intent intent1 = new Intent();
			intent1.setClass(HouseProvingActivity.this,
					InvitecodeRegistActivity.class);
			startActivity(intent1);

			break;
		case R.id.house_btn_next:
			if (!houseID.equals("")) {
				intent.putExtra("houseID", houseID);
				System.out.println("houseID" + houseID);
				intent.setClass(HouseProvingActivity.this,
						RegisterHostActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(HouseProvingActivity.this, "请选择房屋",
						Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.house_edit_city:
			intent = new Intent();
			intent.setClass(HouseProvingActivity.this,
					ChoosecityMainActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.house_edit_community:
			if (!cityID.equals("")) {
				intent.setClass(HouseProvingActivity.this,
						HouseChooseCommunityActivity.class);
				intent.putExtra("cityID", cityID);
				startActivityForResult(intent, 2);
			} else {
				Toast.makeText(HouseProvingActivity.this, "请先选择城市",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.house_edit_housenumber:
			if (!projectID.equals("")) {
				intent.setClass(HouseProvingActivity.this,
						HouseChooseReuseActivity.class);
				intent.putExtra("projectID", projectID);
				startActivityForResult(intent, 3);
			} else {
				Toast.makeText(HouseProvingActivity.this, "请先选择小区",
						Toast.LENGTH_SHORT).show();
			}

			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键
			if (UserMessageActivity.INTENT_FLAG == 0) {
				intentActivity(HouseProvingActivity.this, LoginActivity.class);
			} else if (UserMessageActivity.INTENT_FLAG == 1) {
				intentActivity(HouseProvingActivity.this,
						HouseManageListActivity.class);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			if (data != null) {
				bundle = data.getExtras(); // data为B中回传的Intent
				String str2 = bundle.getString("cityName");// str即为回传的值
				cityID = bundle.getString("cityID");
				edit_city.setText(str2);
				edit_community.setText("");
				edit_housenumber.setText("");
				projectID = "";
				houseID = "";
				projectStr = "";
				houseStr = "";
			}
			break;
		case 2:
			if (data != null) {
				bundle = data.getExtras(); // data为B中回传的Intent
				projectStr = bundle.getString("projectName");// str即为回传的值
				projectID = bundle.getString("projectID");
				edit_community.setText(projectStr);
			}
			break;

		case 3:
			if (data != null) {
				bundle = data.getExtras(); // data为B中回传的Intent
				houseStr = bundle.getString("houseFullName");// str即为回传的值
				houseID = bundle.getString("houseID");
				edit_housenumber.setText(houseStr);
			}
			break;
		default:
			break;
		}
	}

}
